package com.zzb.servlet;


import com.zzb.model.*;
import com.zzb.service.CompanyService;
import com.zzb.service.NavigationService;
import com.zzb.service.NewsService;
import com.zzb.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.处理乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

        //2.得到前端数据

        //3.将数据封装到model

        //4.处理服务逻辑关系
        //顶部导航栏
        NavigationService navigationService = new NavigationService();
        //底部公司信息
        CompanyService companyService = new CompanyService();
        //首页新闻
        NewsService newsService = new NewsService();
        //得到产品信息
        ProductService productService = new ProductService();


        //5.得到处理返回值
        //得到导航栏数据
        List<NavigationModel> navigationModelList = navigationService.getNavigationModel();
        //得到底部公司信息数据
        CompanyModel companyModel = companyService.getCompanyModel();
        //得到首页新闻的数据
        List<NewsModel> indexNewsModelList1 = newsService.getNewsModelList("公司新闻",2);
        List<NewsModel> indexNewsModelList2 = newsService.getNewsModelList("媒体报道",3);
        //得到产品信息
        List<ProductModel> indexProductModelList = productService.getProductModelList();
        //得到产品类型名称
        List<ProductTypeModel> productTypeModelsList = productService.getProductTypeModelList();

        HttpSession session = req.getSession();
        //session.getId();
        session.setAttribute("navigationModelList",navigationModelList);
        session.setAttribute("companyModel",companyModel);

        //req.setAttribute("navigationModelList", navigationModelList);
        //req.setAttribute("companyModel", companyModel);
        req.setAttribute("indexNewsModelList1", indexNewsModelList1);
        req.setAttribute("indexNewsModelList2", indexNewsModelList2);
        req.setAttribute("indexProductModelList", indexProductModelList);
        req.setAttribute("productTypeModelsList", productTypeModelsList);



        //6.将数据返回到前端
        req.getRequestDispatcher("/jsp/index.jsp").forward(req, resp);
    }
}
